﻿function CardMoving() {
    var _availableSlots = null;
    var _selectedCard = null;

    this.GetSelectedCard = function () {
        return _selectedCard;
    };

    function SelectCard() {
        /**/console.log('Select card');

        $(_selectedCard).closest('li').addClass('selected-card');
        HighlightAvailableSlots();

        function HighlightAvailableSlots() {
            _availableSlots = $('#arena ul.warriors li').find('.empty.on').closest('li');
            _availableSlots.addClass('highlight-slot');
            _availableSlots.on('click', ClickToSlot);
        };
    };

    function UnselectCard() {
        /**/console.log('Unselect card');

        $(_selectedCard).closest('li').removeClass('selected-card');
        UnhighlightAvailableSlots();

        function UnhighlightAvailableSlots() {
            _availableSlots.removeClass('highlight-slot');
            _availableSlots.off('click', ClickToSlot);
            _availableSlots = null;
        };
    };

    this.ClickToCard = function () {
        if (_selectedCard != null) {
            UnselectCard();
            _selectedCard = null;
        }

        if ($(this).closest('li:not(.selected-card)')) {
            _selectedCard = this;
            SelectCard();
        }
    };

    function ClickToSlot () {
        /**/console.log('Click to slot');

        UnselectCard();
        MoveCardToTable(_selectedCard, this);
        _selectedCard = null;
    };

    function MoveCardToTable(card, slot) {
        // add card to slot
        $('>*', slot).remove();
        $(slot).html($(card).clone());

        // remove card from hand
        $(card).attr('class', 'card empty on');
        $('>*', card).remove();
    };
};



var moving = new CardMoving();
$('#cards .card.on:not(.empty)').live('click', moving.ClickToCard);